home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / bbs / termv4.6 / extras / source / gtlayout-source.lha / LT_Fixed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  4.3 KB  |  201 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. #ifdef DO_FRACTION_KIND
  15. /****** gtlayout.library/LT_Fixed2String ******************************************
  16. *
  17. *   NAME
  18. *    LT_Fixed2String -- Convert a fixed-point number into an alphanumeric
  19. *                       string.
  20. *
  21. *   SYNOPSIS
  22. *    LT_Fixed2String(Fixed,String);
  23. *                      D0    D1
  24. *
  25. *    VOID LT_Fixed2String(FIXED,STRPTR);
  26. *
  27. *   FUNCTION
  28. *    This routine implements a service similar to ftoa(). It converts
  29. *    a fixed point number into an ASCII representation.
  30. *
  31. *   INPUTS
  32. *    Fixed - Fixed point numeric value
  33. *
  34. *    String - Buffer to receive the converted value. This buffer
  35. *        must be at least 12 bytes large.
  36. *
  37. *   NOTES
  38. *    The decimal point will be taken from the current locale.
  39. *    If locale.library is not installed in your system, the
  40. *    `.' character will be used instead.
  41. *
  42. *    The arguments are passed in D0 and D1, this is *not* a
  43. *    typo.
  44. *
  45. *   BUGS
  46. *    Works reliably, but the entire FIXED datatype operations
  47. *    are broken due to design faults.
  48. *
  49. *   SEE ALSO
  50. *    gtlayout.library/LT_String2Fixed
  51. *
  52. ******************************************************************************
  53. *
  54. */
  55.  
  56. VOID LIBENT
  57. LT_Fixed2String(_REG(d0) FIXED fixed,_REG(d1) STRPTR buffer)
  58. {
  59.     SPrintf(buffer,"%ld%lc%ld",(LONG)(fixed / FIXED_UNITY),(LONG)(LTP_Locale ? LTP_Locale->loc_DecimalPoint[0] : '.'),(LONG)(fixed % FIXED_UNITY));
  60. }
  61. #endif
  62.  
  63.  
  64. /*****************************************************************************/
  65.  
  66.  
  67. #ifdef DO_FRACTION_KIND
  68.  
  69. /****** gtlayout.library/LT_String2Fixed ******************************************
  70. *
  71. *   NAME
  72. *    LT_String2Fixed -- Convert an ASCII string into a fixed-point numeric
  73. *                       value.
  74. *
  75. *   SYNOPSIS
  76. *    Fixed = LT_String2Fixed(String)
  77. *      D0                      A0
  78. *
  79. *    FIXED LT_String2Fixed(STRPTR);
  80. *
  81. *   FUNCTION
  82. *    This routine complements LT_Fixed2String, it implements a
  83. *    service similar to atof() in converting an ASCII string
  84. *    into a fixed-point number.
  85. *
  86. *   INPUTS
  87. *    String - Pointer to null-terminated string.
  88. *
  89. *   RESULT
  90. *    Fixed - Fixed-point number; will be 0.0 if non-numeric
  91. *        characters are found in the input string.
  92. *
  93. *   NOTES
  94. *    The decimal point to look for will be taken from the
  95. *    current locale settings. If locale.library is not
  96. *    installed, the `.' character will be used instead.
  97. *
  98. *   BUGS
  99. *    Works reliably, but the entire FIXED datatype operations
  100. *    are broken due to design faults.
  101. *
  102. ******************************************************************************
  103. *
  104. */
  105.  
  106. FIXED LIBENT
  107. LT_String2Fixed(_REG(a0) STRPTR buffer)
  108. {
  109.     UBYTE    localBuffer[20];
  110.     LONG    decimalPoint;
  111.     LONG    i,left,right;
  112.  
  113.     if(LTP_Locale)
  114.         decimalPoint = LTP_Locale->loc_DecimalPoint[0];
  115.     else
  116.         decimalPoint = '.';
  117.  
  118.     strcpy(localBuffer,buffer);
  119.  
  120.     i = 0;
  121.  
  122.     while(buffer[i])
  123.     {
  124.         if(localBuffer[i] == decimalPoint)
  125.         {
  126.             localBuffer[i] = 0;
  127.  
  128.             if(i)
  129.                 left = LTP_Atol(localBuffer);
  130.             else
  131.                 left = 0;
  132.  
  133.             if(localBuffer[i + 1])
  134.             {
  135.                 UBYTE rest[4];
  136.  
  137.                 CopyMem(&localBuffer[i + 1],rest,3);
  138.  
  139.                 rest[3] = 0;
  140.  
  141.                 right = LTP_Atol(rest);
  142.             }
  143.             else
  144.                 right = 0;
  145.  
  146.             return((FIXED)(left * FIXED_UNITY + right));
  147.         }
  148.  
  149.         i++;
  150.     }
  151.  
  152.     left = LTP_Atol(localBuffer);
  153.  
  154.     return((FIXED)(left * FIXED_UNITY));
  155. }
  156. #endif
  157.  
  158.  
  159. /*****************************************************************************/
  160.  
  161.  
  162. #ifdef DO_FRACTION_KIND
  163. /****** gtlayout.library/LT_FixedMult ******************************************
  164. *
  165. *   NAME
  166. *    LT_FixedMult -- Multiply a fixed-point number with an integer value.
  167. *
  168. *   SYNOPSIS
  169. *    Fixed = LT_FixedMult(Fixed,Factor)
  170. *      D0                   D0    D1
  171. *
  172. *    FIXED LT_FixedMult(FIXED,ULONG);
  173. *
  174. *   FUNCTION
  175. *    The layout engine provides only a fixed point multiplication
  176. *    routine. You need to implement other numeric operations on
  177. *    your own.
  178. *
  179. *   INPUTS
  180. *    Fixed - Fixed-point number to be multiplied.
  181. *
  182. *    Factor - Integer value to multiply fixed-point number with.
  183. *
  184. *   RESULT
  185. *    Fixed - Product
  186. *
  187. *   BUGS
  188. *    Works reliably, but the entire FIXED datatype operations
  189. *    are broken due to design faults.
  190. *
  191. ******************************************************************************
  192. *
  193. */
  194.  
  195. ULONG LIBENT
  196. LT_FixedMult(_REG(d0) FIXED fixed,_REG(d1) ULONG factor)
  197. {
  198.     return((fixed * factor) / FIXED_UNITY);
  199. }
  200. #endif
  201.